﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Survey.Entity;
using Survey.DataAccessLayer;
using WebUI.dto;
using WebUI.constant;
using WebUI.Units;

namespace WebUI.Business
{
    /// <summary>
    /// 重构业务逻辑
    /// </summary>
    public class UserBusiness
    {
        #region 用户管理
        private UserAccessor userAccessor = new UserAccessor();
        // Api/LoginUser
        public Object LoginUser(HttpSessionStateBase session, User user)
        {
            if (String.IsNullOrEmpty(user.PasswordHash)  || String.IsNullOrEmpty(user.Email))
            {
                // return new { Result = false, Msg = "密码不能为空" };
                return Result<User>.Build(
                    CodeEnum.EMAIL_OR_PASSWORD_EMPTY
                    , CodeDictionary.GetCodeMsg(CodeEnum.EMAIL_OR_PASSWORD_EMPTY)
                    , null
                    , false);
            }


            //验证失败将返回异常
            try
            {
                User tempUser = userAccessor.UserValidation(user.Email, user.PasswordHash);
                session["curentUserId"] = tempUser.Id;
                //return new { Result = true };
                return Result<User>.Build(CodeEnum.LOGIN_SUCCESS
                    , CodeDictionary.GetCodeMsg(CodeEnum.LOGIN_SUCCESS)
                    , null
                    , true);
            }
            catch (System.Exception e)
            {
                // return new { Result = false, Msg = "登陆失败" };
                return Result<User>.Build(CodeEnum.LOGIN_FAILED
                    , CodeDictionary.GetCodeMsg(CodeEnum.LOGIN_FAILED)
                    , null
                    , false);
            }
        }
        // Api/EditUser
        public Object EditUser(HttpSessionStateBase session, User tempUser)
        {
            //密码和邮箱不能为空
            if (string.IsNullOrEmpty(tempUser.PasswordHash) || string.IsNullOrEmpty(tempUser.Email))
            {
                // return new { Result = false };
                return Result<User>.Build(CodeEnum.EMAIL_OR_PASSWORD_EMPTY
                    , CodeDictionary.GetCodeMsg(CodeEnum.EMAIL_OR_PASSWORD_EMPTY)
                    , null
                    , false);
            }


            var curentUserId = session["curentUserId"];
            //没有登陆
            if (curentUserId == null)
            {
                //return new { Result = false, Msg = "没有登陆" };
                return Result<User>.Build(CodeEnum.NOT_LOGIN
                    , CodeDictionary.GetCodeMsg(CodeEnum.NOT_LOGIN)
                    , null
                    , false);
            }
            try
            {

                User user = userAccessor.GetUser((int)curentUserId);
                user.Email = tempUser.Email;
                userAccessor.ModifyPassword(user, tempUser.PasswordHash);
                //return new { Result = isOk, Msg = "编辑成功" };
                return Result<User>.Build(
                    CodeEnum.EDITE_SUCCESS
                    , CodeDictionary.GetCodeMsg(CodeEnum.EDITE_SUCCESS)
                    , null
                    , true);
            }
            catch (Exception)
            {

                // return new { Result = false, Msg = e.Message };
                return Result<User>.Build(CodeEnum.EDITE_FAILED
                    , CodeDictionary.GetCodeMsg(CodeEnum.EDITE_FAILED)
                    , null
                    , false);

            }

        }
        //Api/regUser
        public Object RegUser(User user)
        {
            if (Units.DataProcess.EmailRegexCheck(user.Email))
            {
                //return new { Result = false, Msg = "邮箱不正确" };
                return Result<User>.Build(CodeEnum.EMAIL_FORMAT_ERROR
                    , CodeDictionary.GetCodeMsg(CodeEnum.EMAIL_FORMAT_ERROR)
                    , null
                    , false);
            }
            if (Units.DataProcess.PaswordRegexCheck(user.PasswordHash))
            {
                //return new { Result = false, Msg = "密码过于简单，必须由单词字符+某些特殊字符组合" };
                return Result<User>.Build(CodeEnum.PASSWORD_TOO_EASY
                    , CodeDictionary.GetCodeMsg(CodeEnum.PASSWORD_TOO_EASY)
                    , null
                    , false);
            }

            if (String.IsNullOrEmpty(user.Email) || String.IsNullOrEmpty(user.PasswordHash))
            {
                return Result<User>.Build(
                   CodeEnum.EMAIL_OR_PASSWORD_EMPTY
                   , CodeDictionary.GetCodeMsg(CodeEnum.EMAIL_OR_PASSWORD_EMPTY)
                   , null
                   , false);
            }
            try
            {

                userAccessor.Add(user.Email, user.PasswordHash, new List<Questionnaire>());
                userAccessor.Save();
                //return new { Result = isOk, Msg = "成功" };
                return Result<User>.Build(
                  CodeEnum.REG_SUCCESS
                  , CodeDictionary.GetCodeMsg(CodeEnum.REG_SUCCESS)
                  , null
                  , true);
            }
            catch (Exception e)
            {
                return Result<User>.Build(
                  CodeEnum.REG_FAILED
                  , CodeDictionary.GetCodeMsg(CodeEnum.REG_FAILED)
                  , null
                  , false);
            }
        }
        //api/LoginOutUser
        public Object LoginOutUser(HttpSessionStateBase session)
        {
            //获取当前用户的在Session里的Id
            var curentUserId = session["curentUserId"];
            if (curentUserId == null)
            {
                return Result<User>.Build(CodeEnum.NOT_LOGIN
                    , CodeDictionary.GetCodeMsg(CodeEnum.NOT_LOGIN)
                    , null
                    , false);
            }
            //注销成功
            session["curentUserId"] = null;
            return Result<User>.Build(CodeEnum.LOGINOUT_SUCCESS
            , CodeDictionary.GetCodeMsg(CodeEnum.LOGINOUT_SUCCESS)
            , null
            , true);
        }
        // api/DetailedUser
        public Object DetailedUser(HttpSessionStateBase session)
        {
            var curentUserId = session["curentUserId"];

            if (curentUserId == null)
            {
                return Result<User>.Build(CodeEnum.SELECT_NOTHING
                                , CodeDictionary.GetCodeMsg(CodeEnum.SELECT_NOTHING)
                                , null
                                , false);
            }

            try
            {
                var user = userAccessor.GetUser((int)curentUserId);
                user.PasswordHash = "";
                var result = Units.DataProcess.Obj2Json(user);
                    return Result<User>.Build(CodeEnum.SELECT_SUCCESS
                             , CodeDictionary.GetCodeMsg(CodeEnum.SELECT_SUCCESS)
                              ,(User) Units.DataProcess.Json2Obj(result,(new User()).GetType())
                             , true);
            }
            catch (Exception)
            {
                return Result<User>.Build(CodeEnum.SELECT_NOTHING
            , CodeDictionary.GetCodeMsg(CodeEnum.SELECT_NOTHING)
            , null
            , false);
            }

        }
        #endregion
    }
}